package pe.gob.osinergmin.interfase_siaf.dac.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import pe.gob.osinergmin.interfase_siaf.dac.dao.PresupuestoDao;
import pe.gob.osinergmin.interfase_siaf.dac.domain.Gasto;

@Repository("presupuestoDao")
public class PresupuestoDaoImpl implements PresupuestoDao {
	
   private final Log logger = LogFactory.getLog(getClass());
	
	
	private NamedParameterJdbcTemplate jdbcParameterTemplateData;	
	//private JdbcTemplate jdbcTemplateData;	
	
	@Autowired
	public void SetDataSource(DataSource dataSourceData){
		//this.jdbcTemplateData = new JdbcTemplate(dataSourceData);
		this.jdbcParameterTemplateData = new NamedParameterJdbcTemplate(dataSourceData);
		
	}
	
	/**DECLARACION DE SQL**/

	private static final String SQL_SELECT_FASE_PARAMETER ="SELECT * FROM gasto WHERE ano_eje =:anoEje "
			+ " AND sec_func=:secFunc AND id_clasifi=:idClasifi";
	
	private static final String SQL_SELECT_FASE_PARAMETER1 ="SELECT * FROM gasto WHERE ano_eje =:anoEje "
			+ " AND sec_func=:secFunc";
	
	private static final String SQL_SELECT_FASE_PARAMETER2 ="SELECT * FROM gasto WHERE ano_eje =:anoEje "
			+ " AND id_clasifi=:idClasifi";
	
	private static final String SQL_SELECT_FASE_PARAMETER3 ="SELECT * FROM gasto WHERE ano_eje =:anoEje ";
			

	

	
	
	@Override
	public List<Gasto> buscarPresupuesto(Gasto gasto) throws SQLException{		
		logger.info("Ingresando a bucar presupuesto"); 
		SqlParameterSource parameters = new BeanPropertySqlParameterSource(gasto);
		RowMapper<Gasto> lista = ParameterizedBeanPropertyRowMapper.newInstance(Gasto.class);	
		if(StringUtils.isNoneBlank(gasto.getAnoEje()) && 
				StringUtils.isNoneBlank(gasto.getSecFunc()) && 
				StringUtils.isNoneBlank(gasto.getIdClasifi())){ 
			return this.jdbcParameterTemplateData.query(SQL_SELECT_FASE_PARAMETER, parameters, lista);	
		}if(StringUtils.isNoneBlank(gasto.getAnoEje()) && 
				StringUtils.isNoneBlank(gasto.getSecFunc())){ 
			return this.jdbcParameterTemplateData.query(SQL_SELECT_FASE_PARAMETER1, parameters, lista);	
		}else if(StringUtils.isNoneBlank(gasto.getAnoEje()) && 
				StringUtils.isNoneBlank(gasto.getIdClasifi())){
			return this.jdbcParameterTemplateData.query(SQL_SELECT_FASE_PARAMETER2, parameters, lista);	
		}else if(StringUtils.isNoneBlank(gasto.getAnoEje()) && 
				StringUtils.isBlank(gasto.getSecFunc()) && 
				StringUtils.isBlank(gasto.getIdClasifi())){ 
			return this.jdbcParameterTemplateData.query(SQL_SELECT_FASE_PARAMETER3, parameters, lista);	
		}else{
			return null;
		}			
	}
	
	

}
